#include<bits/stdc++.h>

using namespace std;


FILE *fp;
char cbuffer;
char *key[8] = {"if", "else", "for", "while", "int", "return", "break", "continue"};
char *border[6] = {",", ";", "{", "}", "(", ")"};
char *arithmetic[5] = {"+", "-", "*", "/", "="};
char *relation[6] = {"<", "<=", "==", ">", ">=", "!="};
char *consts[20];
char *label[20];
int constnum = 0, labelnum = 0;

int search(char searchchar[], int wordtype) {
    int i = 0;
    switch (wordtype) {
        case 1:
            for (i = 0; i <= 7; i++)
                if (strcmp(key[i], searchchar) == 0) return (i + 1);
            return 0;
        case 2:
            for (i = 0; i <= 5; i++)
                if (strcmp(border[i], searchchar) == 0) return (i + 1);
            return (0);
        case 3:
            for (i = 0; i <= 4; i++)
                if (strcmp(arithmetic[i], searchchar) == 0) return (i + 1);
            return (0);
        case 4:
            for (i = 0; i <= 5; i++)
                if (strcmp(relation[i], searchchar) == 0) return (i + 1);
            return (0);
        case 5:
            for (i = 0; i < constnum; i++)
                if (strcmp(consts[i], searchchar) == 0) return (i + 1);
            consts[i] = (char *) malloc(sizeof(searchchar));
            strcpy(consts[i], searchchar);
            constnum++;
            return (i + 1);
        case 6:
            for (i = 0; i < labelnum; i++)
                if (strcmp(label[i], searchchar) == 0) return (i + 1);
            label[i] = (char *) malloc(sizeof(searchchar));
            strcpy(label[i], searchchar);
            labelnum++;
            return (i + 1);
    }
}


char alphaprocess(char buffer)//字母开头串的处理（可能是保留字或标识符）
{
    int atype;
    int i = -1;
    char alphatp[20];
    while ((isalpha(buffer)) || (isdigit(buffer))) {
        alphatp[++i] = buffer;
        buffer = fgetc(fp);
    }
    alphatp[i + 1] = '\0';
    //printf("%s,search=%d",alphatp,search(alphatp,1));
    if (atype = search(alphatp, 1))
        printf("(%s,1,%d)\n", alphatp, atype);
    else {
        atype = search(alphatp, 6);
        printf("(%s,6,%d)\n", alphatp, atype);
    }
    return (buffer);
}

char digitprocess(char buffer)  //常量串
{
    int i = -1;
    char digittp[20];
    int dtype;
    while ((isdigit(buffer))) {
        digittp[++i] = buffer;
        buffer = fgetc(fp);
    }
    digittp[i + 1] = '\0';
    dtype = search(digittp, 5);
    printf("(%s,5,%d)\n", digittp, dtype);
    return (buffer);
}

char otherprocess(char buffer)  //其它符号的处理
{

    int i = -1;
    char othertp[20];
    int otype, otypetp;
    othertp[0] = buffer;
    othertp[1] = '\0';
    if (otype = search(othertp, 3)) {
        printf("(%s,3,%d)\n", othertp, otype);
        buffer = fgetc(fp);
        goto out;
    }
    if (otype = search(othertp, 4)) {
        buffer = fgetc(fp);
        othertp[1] = buffer;
        othertp[2] = '\0';
        if (otypetp = search(othertp, 4)) {
            printf("(%s,4,%d)\n", othertp, otypetp);
            goto out;
        } else {
            othertp[1] = '\0';
            printf("(%s,4,%d)\n", othertp, otype);
            goto out;
        }
    }
    if (buffer == ':') {
        buffer = fgetc(fp);
        if (buffer == '=')
            printf(":= (2,2)\n");
        buffer = fgetc(fp);
        goto out;
    } else if (otype = search(othertp, 2)) {
        printf("(%s,2,%d)\n", othertp, otype);
        buffer = fgetc(fp);
        goto out;
    }

    if ((buffer != '\n') && (buffer != ' '))
        printf("%c error,not a word\n", buffer);
    buffer = fgetc(fp);
    out:
    return (buffer);
}

int main() {

    int i;
    for (i = 0; i <= 20; i++) {
        label[i] = NULL;
        consts[i] = NULL;
    };
    if ((fp = fopen("input.cpp", "r")) == NULL)
        printf("Open File Error!");
    else {
       // printf("输出格式为：(单词，类别，位置)\n");
        cbuffer = fgetc(fp);
        while (cbuffer != EOF) {
            if (isalpha(cbuffer))
                cbuffer = alphaprocess(cbuffer);
            else if (isdigit(cbuffer))
                cbuffer = digitprocess(cbuffer);
            else cbuffer = otherprocess(cbuffer);
        }
        printf("Anysis End\n");
        getchar();
    }
}